package com.skcc.cip.ngms.temp;

import java.io.InputStream;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLStreamReader;

import org.apache.axiom.om.impl.builder.StAXBuilder;
import org.apache.axiom.om.util.StAXUtils;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
import org.apache.axis2.AxisFault;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.converter.stream.InputStreamCache;
import org.w3c.dom.Document;



public class XmlConverter implements Processor{

	public void process(Exchange exchange) throws Exception {
		// TODO Auto-generated method stub
		InputStreamCache is = (InputStreamCache) exchange.getIn().getBody();
		org.apache.axiom.soap.SOAPEnvelope env = createSOAPEnvelope(is);
		XMLStreamReader reader = env.getBody().getFirstElement().getXMLStreamReader();
		
		Stax2DomBuilder builder = new Stax2DomBuilder();
		
        Document domDoc = builder.build(reader);
        System.out.println(domDoc.toString());
		Document docu = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
		
//		XMLOutputter xo = new XMLOutputter();
//		String str = xo.outputString((org.jdom.Document) docu);
	}
	
	private SOAPEnvelope createSOAPEnvelope(InputStream in) throws AxisFault {
        try {
            XMLStreamReader xmlreader = StAXUtils.createXMLStreamReader(in);
            StAXBuilder builder = new StAXSOAPModelBuilder(xmlreader, null);
            return (SOAPEnvelope) builder.getDocumentElement();
        } catch (Exception e) {
            throw new AxisFault(e.getMessage(), e);
        }
    }
	
}

